package com.yinhe.music.yhmusic.dialog;

import android.content.Context;
import android.support.v7.app.AlertDialog;

import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.downmusic.DownloadStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建歌单弹窗
 */
public class DeleteDialog {

    private Context mContext;
    private AlertDialog.Builder mDialog;
    private List<Integer> deleteList;
    private List<Integer> hasDownList;
    private boolean deleteFile;

    DeleteDialogListener mListener;


    public interface DeleteDialogListener {
        void deleteDialogDidConfirm( List<Integer> hasDownList, boolean deleteFile);
    }

    public DeleteDialog(Context mContext, List<Integer> deleteList,DeleteDialogListener mListener) {
        this.mContext = mContext;
        this.deleteList = deleteList;
        this.mListener = mListener;
        init();
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    private void init() {
        mDialog = new AlertDialog.Builder(mContext);
        mDialog.setTitle("确定将所选音乐从列表中删除？");
        getDownList(deleteList);
        setConfirmDelete();
        mDialog.setNegativeButton("取消", null);
    }

    private void getDownList(List<Integer> songList) {
        if (songList == null) {
            return;
        }
        List<Integer> hasDownList = new ArrayList<>();
        for (int id : songList) {
            boolean hasDown = DataBaseAccessor.getInstance().isExsitIdSyn(id, DownloadStatus.DOWNLOAD_STATUS_COMPLETED);
            if (hasDown) {
                hasDownList.add(id);
            }
        }

        if (!hasDownList.isEmpty()) {
            showDeleteDownMusic(hasDownList);
        }
    }

    private void showDeleteDownMusic(List<Integer> hasDownList) {
        mDialog.setMultiChoiceItems(new String[]{ "同时删除下载文件" }, new boolean[]{ false },
                (dialogInterface, i, b) -> deleteFile = b);
        this.hasDownList = hasDownList;
    }

    private void setConfirmDelete() {
        mDialog.setPositiveButton("确定", (dialog, which) -> {
            if (mListener != null) {
                mListener.deleteDialogDidConfirm(hasDownList, deleteFile);
            }
        });

    }
}
